package com.example.web.server.impl;

import java.io.File;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.example.web.server.EmailService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class SendTemplate {

	@Autowired
	private JavaMailSender mailSender;
	@Autowired
	private EmailService emailService;

	@Value("${spring.mail.username}")
	private String mailFrom;

	public boolean sendResetHtml(String subject, String to) {
		String body = emailService.Send(subject, to);
		if (body != null) {
			return sendMailHtml(to, subject, body, true);
		}
		return false;
	}

	public boolean sendResetImg(String subject, String to) {
		return sendMailImg(to, subject, true);
	}

	public boolean sendResetText(String subject, String to) {
		String body = "在一个风雨交加的夜晚，他和她xxxxx";
		return sendMailText(to, subject, body, true);
	}

	public boolean sendResetFiles(String subject, String to) {
		return sendMailFile(to, subject, "文件", true);
	}

	/**
	 * html
	 * 
	 * @param to      收件人邮箱
	 * @param subject 标题
	 * @param body    内容
	 * @param isHtml  是否是h
	 * @return
	 */
	private boolean sendMailHtml(String to, String subject, String body, boolean isHtml) {
		try {
			MimeMessage mail = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mail, true);
			helper.setFrom(mailFrom);
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(body, isHtml);
			mailSender.send(mail);
			return true;
		} catch (Exception e) {
			log.error("send mail error", e);
			return false;
		}
	}

	/**
	 *	 图片
	 * 
	 * @param to      收件人邮箱
	 * @param subject 标题
	 * @param body    内容
	 * @param isHtml  是否是h
	 * @return
	 */
	public boolean sendMailImg(String to, String subject, boolean isHtml) {
		try {
			String Id = "888812138";
			String content = "<html><body>这是有图片的邮件：<img src=\'cid:" + Id + "\' ></body></html>";
			String imgPath = "/0.0/1.jpg";
			MimeMessage message = mailSender.createMimeMessage();

			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setFrom(mailFrom);
			helper.setTo("825620905@qq.com");
			helper.setSubject("这是有图片的邮件");
			helper.setText(content, true);

			FileSystemResource res = new FileSystemResource(new File(imgPath));
			helper.addInline(Id, res);

			mailSender.send(message);
			return true;
		} catch (Exception e) {
			log.error("send mail error", e);
			return false;
		}
	}

	/**
	 * 文本
	 * 
	 * @param to      收件人邮箱
	 * @param subject 标题
	 * @param body    内容
	 * @param isHtml  是否是h
	 * @return
	 */
	private boolean sendMailText(String to, String subject, String body, boolean isHtml) {
		try {
			MimeMessage mail = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mail, true);
			helper.setFrom(mailFrom);
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(body);
			mailSender.send(mail);
			return true;
		} catch (Exception e) {
			log.error("send mail error", e);
			return false;
		}
	}

	/**
	 * 附件
	 * 
	 * @param to      收件人邮箱
	 * @param subject 标题
	 * @param body    内容
	 * @param isHtml  是否是h
	 * @return
	 */
	private boolean sendMailFile(String to, String subject, String body, boolean isHtml) {
		// windows系统文件路径
		String filePath = "/0.0/001.mp4";
		try {
			MimeMessage mail = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mail, true);
			helper.setFrom(mailFrom);
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(body, isHtml);
			FileSystemResource file = new FileSystemResource(new File(filePath));
			String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
			helper.addAttachment(fileName, file);
			mailSender.send(mail);
			return true;
		} catch (Exception e) {
			log.error("send mail error", e);
			return false;
		}
	}
}
